package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年11月28日 16:02
 */
public class WeaBfarxHfwcqrAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaBfarxHfwcqrAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            String fullDate = DateUtil.getFullDate();
            rs.executeQuery("select crmsjid,arxwcqk,wcsj,bz,yjwcrq from " + tablename + " where requestid=?",requestid);
            if(rs.next()) {
                String crmsjid = Util.null2String(rs.getString("crmsjid"));//CRM数据ID
                String arxwcqk = Util.null2String(rs.getString("arxwcqk"));//AR项完成情况
                String bz = Util.null2String(rs.getString("bz"));//备注
                String yjwcrq = Util.null2String(rs.getString("yjwcrq"));//预计完成日期
                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "ar_item__c");
                yjwcrq = !"".equals(yjwcrq) ? yjwcrq + " 00:00:00" : yjwcrq;
                object_data.put("expected_completion__c", crmUtil.dateToStamp(yjwcrq));
                object_data.put("actual_reply_time__c", crmUtil.dateToStamp(fullDate));
                object_data.put("completion_state__c", arxwcqk);
                bz = bz.replaceAll("&nbsp;", " ");
                object_data.put("description__c", bz);
                object_data.put("_id", crmsjid);
				dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);

                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaBfarxHfwcqrAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaBfarxHfwcqrAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "WeaBfarxAction");
                writeLog(">>>>>WeaBfarxHfwcqrAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaBfarxAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
